package com.sundae.smarthomedemo.Activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.sundae.smarthomedemo.Beans.DeviceSQLBean;
import com.sundae.smarthomedemo.MyAppConfig;
import com.sundae.smarthomedemo.R;
import com.sundae.smarthomedemo.Utils.DaoControl;
import com.sundae.smarthomedemo.Utils.Utils;
import com.sundae.smarthomedemo.Widget.EditDialog;

public class DeviceInfoActivity extends BaseActivity {

    private Button button;
    private ImageButton back;
    private ImageView img;

    private TextView device_name , device_register_time;

    private boolean isAdd = false;
    private int deviceType = MyAppConfig.rsq_type;
    private Intent intent;
    private View setName_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_info);

        StatusBarUtil.setColor(this , getResources().getColor(R.color.styleColor) , 0);

        intent = getIntent();
        isAdd = intent.getBooleanExtra("isAdd" , true);

        initView();
    }

    private void initView()
    {
        button = (Button) findViewById(R.id.deviceinfo_button);
        back = (ImageButton) findViewById(R.id.deviceinfo_back);
        img = (ImageView) findViewById(R.id.deviceinfo_img);

        setName_view = findViewById(R.id.device_info_layout3);

        device_name = (TextView) findViewById(R.id.deviceinfo_name);
        device_register_time = (TextView) findViewById(R.id.deviceinfo_addtime);

        back = (ImageButton) findViewById(R.id.deviceinfo_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if(isAdd)initAdd();
        else initInfo();
    }

    private DeviceSQLBean deviceSQLBean;
    private void initAdd()
    {
        deviceSQLBean = new DeviceSQLBean();
        deviceType = intent.getIntExtra("deviceType" , 0);

        img.setImageResource(Utils.getImgRes(deviceType));
        device_name.setText("测试用电器001");
        device_register_time.setText(Utils.getNowTime());
        deviceSQLBean.setDevice_register_time(Utils.getNowTime());

        setName_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditDialog editDialog = new EditDialog(DeviceInfoActivity.this);
                editDialog.setOnPosNegClickListener(new EditDialog.OnPosNegClickListener() {
                    @Override
                    public void posClickListener(String value) {
                        deviceSQLBean.setDevice_name(value);
                        device_name.setText(value);
                    }

                    @Override
                    public void negCliclListener(String value) {
                    }
                });
                editDialog.show();
            }
        });

        button.setText("绑定设备");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isAdd)
                {
                    deviceSQLBean.setUsername(MyAppConfig.user_name);
                    deviceSQLBean.setDevice_type(deviceType);
                    deviceSQLBean.setModel_type(0);
                    deviceSQLBean.setPower(false);
                    DaoControl.getInstance(getApplicationContext()).insert(deviceSQLBean);
                }

                startActivity(new Intent(DeviceInfoActivity.this , MainActivity.class));
            }
        });

    }

    private void initInfo()
    {

        img.setImageResource(Utils.getImgRes(selectDevice.getDevice_type()));
        device_name.setText(selectDevice.getDevice_name());
        device_register_time.setText(selectDevice.getDevice_register_time());

        setName_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditDialog editDialog = new EditDialog(DeviceInfoActivity.this);
                editDialog.setOnPosNegClickListener(new EditDialog.OnPosNegClickListener() {
                    @Override
                    public void posClickListener(String value) {
                        selectDevice.setDevice_name(value);
                        DaoControl.getInstance(getApplicationContext()).update(selectDevice);
                        device_name.setText(value);
                    }

                    @Override
                    public void negCliclListener(String value) {
                    }
                });
                editDialog.show();
            }
        });


        button.setText("解绑设备");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DaoControl.getInstance(getApplicationContext()).delete(selectDevice.getId());
                startActivity(new Intent(DeviceInfoActivity.this , MainActivity.class));
            }
        });

    }

}
